package com.stars.empoly.view

import android.content.Context
import android.text.TextUtils
import android.util.AttributeSet
import android.util.Log
import android.view.LayoutInflater
import androidx.databinding.DataBindingUtil
import com.stars.empoly.R
import com.stars.empoly.databinding.TimeDayAndHourBinding
import com.stars.empoly.inte.ChoiceListData
import com.stars.empoly.widget.BaseLinearLayout
import com.trailer.base.view.BView
import com.trailer.base.view.BaseView
import java.lang.String
import java.text.ParseException
import java.text.SimpleDateFormat
import java.util.*



/**
 *
 *on 2020/11/23
 */
class TimeDayAddHourView(context: Context, attrs: AttributeSet) : BaseLinearLayout(context, attrs) {

    var list = ArrayList<kotlin.String>()
    var listTwo = ArrayList<kotlin.String>()
    var listThree = ArrayList<kotlin.String>()
    var listFour = ArrayList<kotlin.String>()
    var listData = ArrayList<kotlin.String>()
    var index = 0
    var indexEnd = 0
    var minteEnd = 0
    var hourEnd = 0



    private var binding: TimeDayAndHourBinding? = null

    private var view: BView? = null

    init {
        init(context)

    }

    private fun init(context: Context) {
        if (context is BaseView) {
            view = context as BView
        }
        binding = DataBindingUtil.inflate(LayoutInflater.from(context), R.layout.time_day_and_hour, this, true)
        setTime()
    }


    var choiceTimeRightData: ChoiceListData? = null
    fun setCallBack(choiceData: ChoiceListData) {
        this.choiceTimeRightData = choiceData
    }


    var flag = true

    fun setFlag() {
        flag = true
    }

    fun setData(day: kotlin.String?, time: kotlin.String?) {
        flag = false

        if (!TextUtils.isEmpty(time)) {

            var ss = time!!.split("-")

            Log.e("-shy-", "ss=: " + ss[0])
            Log.e("-shy-", "ss=: " + ss[1])
            for ((index, name) in listTwo.withIndex()) {
                if (name == ss[0]) {
                    Log.e("-shy-", "2222 ")

                    binding!!.wheel3dTwo.currentIndex = index

                }
            }
            for ((index, name) in listThree.withIndex()) {
                if (name == ss[1]) {
                    Log.e("-shy-", "3333 ")

                    binding!!.wheel3dFour.currentIndex = index


                }
            }

        }

        if (!TextUtils.isEmpty(day)) {

            for ((index, name) in listData.withIndex()) {
                if (name == day) {

                    binding!!.wheel3d.currentIndex = index

                    return
                }
            }


        }


    }

    fun getTimeOne(): kotlin.String {

        return list[0]


    }

    //设置编辑的默认时间
    fun  setTimeStart(data: kotlin.String, time: kotlin.String){

        //重新设置
        initHourData(data)



        for ((index, value) in listData.withIndex()) {
            if(value == data){
                binding!!.wheel3d.currentIndex=index
            }
        }

        //时间
        var ss=time.split(":")
        var s1=ss[0]

        var s2=ss[1]

        for ((index, value) in listTwo.withIndex()) {
            if(value == s1){
                binding!!.wheel3dTwo.currentIndex=index

            }

        }

        for ((index, value) in listFour.withIndex()) {
            if(value == s2){
                binding!!.wheel3dSix.currentIndex=index
                return
            }

        }

    }

    fun  setTimeEnd(data: kotlin.String, time: kotlin.String){

        for ((index, value) in listData.withIndex()) {
            if(value == data){
                binding!!.wheel3dFive.currentIndex=index
            }
        }

        //时间
        var ss=time.split(":")
        var s1=ss[0]

        var s2=ss[1]

        for ((index, value) in listTwo.withIndex()) {
            if(value == s1){
                binding!!.wheel3dFour.currentIndex=index

            }

        }

        for ((index, value) in listFour.withIndex()) {
            if(value == s2){
                binding!!.wheel3dSeven.currentIndex=index
                return
            }

        }


    }



    fun getTimeOneTo(): kotlin.String {

        return listData[index]

    }

    fun getTimeEnd(): kotlin.String {

        return list[indexEnd]

    }

     fun getTimeEndTo(): kotlin.String {

         return listData[indexEnd]

     }
    fun getMinuteStart(): kotlin.String {


        return listFour[minteEnd]

    }

    fun getMinuteEnd(): kotlin.String {

        return listFour[minteEnd]

    }



    fun getTimeTwo(): kotlin.String {


        return listTwo[hourEnd]
    }

    fun getTimeThree(): kotlin.String {

        return listThree[hourEnd]

    }

    private fun setTime() {


        initHour()
        initTwo()
        initText()
        initThree()

        //分钟
        initSix()



//        binding!!.one.currentItem = 0
//        binding!!.one.visibleItems = 6
//
//        binding!!.two.currentItem = 0
//        binding!!.two.visibleItems = 6
//
//        binding!!.text.currentItem = 0
//        binding!!.text.visibleItems = 6
//
//        binding!!.three.currentItem = 0
//        binding!!.three.visibleItems = 6

    }


    fun getOldDate(distanceDay: Int, flag: Boolean): kotlin.String {
        val dft = SimpleDateFormat("yyyy-MM-dd")
        val beginDate = Date()
        val date = Calendar.getInstance()
        date.time = beginDate
        if(flag) {
            date[Calendar.DATE] = date[Calendar.DATE] + distanceDay
        }else{
            date[Calendar.DATE] = date[Calendar.DATE] - distanceDay
        }
        var endDate: Date? = null
        try {
            endDate = dft.parse(dft.format(date.time.time))
        } catch (e: ParseException) {
            e.printStackTrace()
        }
        return dft.format(endDate)
    }

    fun getOldDateTo(distanceDay: Int, flag: Boolean): kotlin.String {
        val dft = SimpleDateFormat("MM月dd日")
        val beginDate = Date()
        val date = Calendar.getInstance()
        date.time = beginDate
        if(flag) {
            date[Calendar.DATE] = date[Calendar.DATE] + distanceDay
        }else{
            date[Calendar.DATE] = date[Calendar.DATE] - distanceDay
        }
        var endDate: Date? = null
        try {
            endDate = dft.parse(dft.format(date.time))
        } catch (e: ParseException) {
            e.printStackTrace()
        }
        return dft.format(endDate)
    }

    private fun initText() {


        var list = ArrayList<kotlin.String>()
        list.add("到")


        binding!!.wheel3dThree.setEntries(list)


        binding!!.wheel3dThree.setOnWheelChangedListener { view, oldIndex, newIndex ->
            val text = view.getItem(newIndex)

            Log.e("-shy-", "text4=: $text")
            // Log.i("WheelView", String.format("index: %d, text: %s", newIndex, text));


        }

        var list1 = ArrayList<kotlin.String>()
        list1.add(":")

        binding!!.wheel3dStart.setEntries(list1)
        binding!!.wheel3dEnd.setEntries(list1)

        binding!!.wheel3dStart.setOnWheelChangedListener { view, oldIndex, newIndex ->
            val text = view.getItem(newIndex)

            Log.e("-shy-", "text4=: $text")
            // Log.i("WheelView", String.format("index: %d, text: %s", newIndex, text));


        }

        binding!!.wheel3dEnd.setOnWheelChangedListener { view, oldIndex, newIndex ->
            val text = view.getItem(newIndex)

            Log.e("-shy-", "text4=: $text")
            // Log.i("WheelView", String.format("index: %d, text: %s", newIndex, text));


        }






//        val adapter = ArrarTextAdapter(context, list)
//        adapter.textSize = 18
//        adapter.textColor = resources.getColor(R.color.wheel_title)
//
//        binding!!.text.viewAdapter = adapter
//        binding!!.text.isCyclic = false
//        binding!!.text.addScrollingListener(object : OnWheelTimeScrollListener {
//            override fun onScrollingStarted(wheel: WheelTimeView?) {
//
//            }
//
//            override fun onScrollingFinished(wheel: WheelTimeView?) {
//
//
//            }
//
//        })


    }


    private fun initHourData(data: kotlin.String) {
        var nowData=getOldDate(0, true)//当前时间

        val df = SimpleDateFormat("yyyy-MM-dd")
        val now = df.parse(nowData)
        val compare = df.parse(data)

        Log.e("-7777-", "now= " + now)
        Log.e("-7777-", "compare= " + compare)



        if (!now.before(compare)) {//当前时间大于服务器返回时间

            Log.e("-7777-", "0000 ")
            list.clear()
            listData.clear()

            //重新给值

            val diff: Long = now.time - compare.time // 这样得到的差值是微秒级别

            val days = (diff / (1000 * 60 * 60 * 24)).toInt()
            if(days>0) {
                for (index in 1..days) {


                    list.add(getOldDateTo(index, false))
                    listData.add(getOldDate(index, false))

                }
            }

            for (index in 0..30) {

                list.add(getOldDateTo(index, true))
                listData.add(getOldDate(index, true))
            }


            binding!!.wheel3d.setEntries(list)
            binding!!.wheel3dFive.setEntries(list)


            binding!!.wheel3d.setOnWheelChangedListener { view, oldIndex, newIndex ->
                val text = view.getItem(newIndex)

                index = newIndex

                Log.e("-shy-", "text1=: $text")


                // Log.i("WheelView", String.format("index: %d, text: %s", newIndex, text));

                if (null != choiceTimeRightData) {

                    choiceTimeRightData!!.onRestuse("1", text as kotlin.String?)
                }

            }

            binding!!.wheel3dFive.setOnWheelChangedListener { view, oldIndex, newIndex ->
                val text = view.getItem(newIndex)

                indexEnd = newIndex

                Log.e("-shy-", "text4=: $text")


                // Log.i("WheelView", String.format("index: %d, text: %s", newIndex, text));

                if (null != choiceTimeRightData) {

                    choiceTimeRightData!!.onRestuse("4", text as kotlin.String?)
                }

            }

        }else{
            Log.e("-7777-", "11111 ")

        }



    }


    private fun initHour() {


        for (index in 0..30) {

            list.add(getOldDateTo(index, true))
            listData.add(getOldDate(index, true))
        }


        binding!!.wheel3d.setEntries(list)
        binding!!.wheel3dFive.setEntries(list)


        binding!!.wheel3d.setOnWheelChangedListener { view, oldIndex, newIndex ->
            val text = view.getItem(newIndex)

            index = newIndex

            Log.e("-shy-", "text1=: $text")


            // Log.i("WheelView", String.format("index: %d, text: %s", newIndex, text));

            if (null != choiceTimeRightData) {

                choiceTimeRightData!!.onRestuse("1", text as kotlin.String?)
            }

        }

        binding!!.wheel3dFive.setOnWheelChangedListener { view, oldIndex, newIndex ->
            val text = view.getItem(newIndex)

            indexEnd = newIndex

            Log.e("-shy-", "text4=: $text")


            // Log.i("WheelView", String.format("index: %d, text: %s", newIndex, text));

            if (null != choiceTimeRightData) {

                choiceTimeRightData!!.onRestuse("4", text as kotlin.String?)
            }

        }



    }


    private fun initTwo() {


        val mCalendar = Calendar.getInstance()
        val mHour=mCalendar.get(Calendar.HOUR_OF_DAY)

        var choseIndex=0
        for (index in 0..23) {

            if(index==mHour){
                hourEnd=index
                choseIndex=index
            }
            listTwo.add(String.format(Locale.CHINA, "%02d", index))//todo"%02d:00"
        }



        binding!!.wheel3dTwo.setEntries(listTwo)




        binding!!.wheel3dTwo.currentIndex=choseIndex

        binding!!.wheel3dTwo.setOnWheelChangedListener { view, oldIndex, newIndex ->
            val text = view.getItem(newIndex)

            Log.e("-shy-", "text2=: $text")

            if (null != choiceTimeRightData) {

                choiceTimeRightData!!.onRestuse("2", text.toString())

            }
            if (flag) {
                if (newIndex + 1 < listThree.size - 1) {
                    // listThree[newIndex + 1]
                    binding!!.wheel3dFour.currentIndex = newIndex + 1

                } else {
                    binding!!.wheel3dFour.currentIndex = listThree.size - 1
                }

            }


            // Log.i("WheelView", String.format("index: %d, text: %s", newIndex, text));


        }


    }


    private  fun initSix(){

        val mCalendar = Calendar.getInstance()
        val mHour=mCalendar.get(Calendar.MINUTE)
        var choseIndex=0
        for (index in 0..60) {
            if(index==mHour){
                choseIndex=index
                minteEnd=index

            }
            listFour.add(String.format(Locale.CHINA, "%02d", index))//todo:"%02d:00"
        }

        binding!!.wheel3dSix.setEntries(listFour)

        binding!!.wheel3dSeven.setEntries(listFour)


        binding!!.wheel3dSix.currentIndex=choseIndex

        binding!!.wheel3dSeven.currentIndex=choseIndex


        binding!!.wheel3dSix.setOnWheelChangedListener { view, oldIndex, newIndex ->
            val text = view.getItem(newIndex)

            Log.e("-shy-", "text6=: $text")
            if (null != choiceTimeRightData) {

                choiceTimeRightData!!.onRestuse("6", text.toString())

            }


        }

        binding!!.wheel3dSeven.setOnWheelChangedListener { view, oldIndex, newIndex ->
            val text = view.getItem(newIndex)

            Log.e("-shy-", "text6=: $text")
            if (null != choiceTimeRightData) {

                choiceTimeRightData!!.onRestuse("7", text.toString())

            }


        }



    }






    private fun initThree() {

        val mCalendar = Calendar.getInstance()
        val mHour=mCalendar.get(Calendar.HOUR_OF_DAY)
        var choseIndex=0
        for (index in 0..23) {
            if(index==mHour){
                choseIndex=index
            }
            listThree.add(String.format(Locale.CHINA, "%02d", index))//todo:"%02d:00"
        }

        binding!!.wheel3dFour.setEntries(listThree)


        binding!!.wheel3dFour.currentIndex=choseIndex

        binding!!.wheel3dFour.setOnWheelChangedListener { view, oldIndex, newIndex ->
            val text = view.getItem(newIndex)

            Log.e("-shy-", "text4=: $text")
            if (null != choiceTimeRightData) {

                choiceTimeRightData!!.onRestuse("3", text.toString())

            }


        }

    }

}







